package com.sgb.goods.view.adapter.publish.pop;

import android.content.Context;
import android.view.inputmethod.EditorInfo;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.KeyboardUtils;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsPopUnitBinding;
import com.sgb.goods.entity.UnitEntity;
import com.sgb.goods.utils.base.adapter.RecordPointCommonAdapter;
import com.sgb.goods.widget.pop.GoodsPopWindow;
import com.swgk.core.dialog.PopViewHolder;

import java.util.List;

public class UnitPopWindow extends GoodsPopWindow {

    private InputTextInterface inputTextInterface;
    public RecordPointCommonAdapter<UnitEntity> unitAdapter;
    public ObservableField<String> contentText = new ObservableField<>("");

    public UnitPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        unitAdapter = new RecordPointCommonAdapter<UnitEntity>(R.layout.goods_item_unit, BR.viewModel) {
            @Override
            public boolean equivalentToObject(UnitEntity object1, UnitEntity object2) {
                if(null == object1 || null == object2) {
                    return false;
                }
                return object1.id.equals(object2.id);
            }
        };
        return R.layout.goods_pop_unit;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        holder.setBinding(BR.bpw, this);
        initListener((GoodsPopUnitBinding) holder.getmBinding());
    }

    private void initListener(GoodsPopUnitBinding mDataBinding) {
        mDataBinding.etView.setOnEditorActionListener((textView, actionId, keyEvent) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                KeyboardUtils.hideSoftInput(textView);
                inputTextInterface.input(contentText.get());
            }
            return false;
        });
        mDataBinding.okView.setOnClickListener(v -> {
            // 记录本次有操作的记录点；若没操作点击，那么等效于无。
            unitAdapter.setRecordPoint();
            if (onResultClick != null && null != unitAdapter.oldObj) {
                onResultClick.result(this.unitAdapter.oldObj.no, this.unitAdapter.oldObj.name, null);
            }
            dissMiss();
        });
        mDataBinding.resetView.setOnClickListener(v -> {
            // 取消本次有操作的记录点；若没操作点击，那么等效于无。
            unitAdapter.cancelRecordPoint();
            dismiss();
        });
    }

    public void setData(List<UnitEntity> unitEntityList) {
        if(null == unitEntityList) {
            return;
        }
        unitAdapter.setNewData(unitEntityList);
        // 设置记录点
        unitAdapter.setRecordPoint(unitAdapter.oldObj);
    }

    public interface InputTextInterface {
        void input(String s);
    }

    public void inputTextInterface(InputTextInterface inputTextInterface) {
        this.inputTextInterface = inputTextInterface;
    }
}
